library(RWeka)

set.seed(100)
ind <- sample(2, nrow(iris), replace = TRUE, prob = c(0.7, 0.3))
train <- iris[ind==1, ]
test <- iris[ind==2, ]

flower_model <- Species ~ Sepal.Length + Sepal.Width + Petal.Length + Petal.Width

c45_iris_tree <-
  J48(flower_model, data = train, 
      control = Weka_control(U = TRUE))

c45_iris_tree2 <- J48(flower_model, data = train,
                      control = Weka_control(U=T, M=5))

c45_iris_tree3 <- J48(flower_model, data = train,
                      control = Weka_control(C=0.05))

c45_iris_tree4 <- J48(flower_model, data = train,
                      control = Weka_control(R=T, N=4))
summary(c45_iris_tree4)


c45_iris_tree5 <- J48(flower_model, data = train,
                      control = Weka_control(C=0.1, M=5))
summary(c45_iris_tree5)

#####################
iris2 <- subset(iris, iris$Species != "virginica")

library(rpart)

cart1 <- rpart(flower_model, data = train, 
               control = rpart.control(maxdepth=10, xval=10))

cart1

prune(cart1, cp=0.015)

plot(cart1)

text(cart1, all = TRUE, digits = 7, use.n = TRUE, cex = 0.9, xpd = TRUE)

cart_testPredict <- predict(cart1, newdata = test)
table(cart_testPredict, test$Species)

